package com.shuorikeji.huohuo.utils;

import android.content.Context;
import android.os.Handler;
import android.util.Log;

import com.bytedance.sdk.djx.DJXSdk;
import com.bytedance.sdk.djx.DJXSdkConfig;
import com.bytedance.sdk.djx.internal.utils.MetaDataUtils;
import com.bytedance.sdk.djx.model.DJXError;
import com.bytedance.sdk.dp.DPSdk;
import com.bytedance.sdk.dp.DPSdkConfig;
import com.bytedance.sdk.nov.api.NovSdk;
import com.bytedance.sdk.nov.api.NovSdkConfig;
import com.bytedance.sdk.openadsdk.TTAdConfig;
import com.bytedance.sdk.openadsdk.TTAdSdk;
import com.shuorikeji.huohuo.common.Constant;

public class InitUtils {
    public static final boolean debug = true;
    private static final String TAG = "InitUtils";

    public static void csj(Context context, InitCallback callback) {
        if (TTAdSdk.isSdkReady()) {
            callback.success();
            return;
        }

        TTAdSdk.init(context, new TTAdConfig.Builder().appId(Constant.CSJ_APP_ID) //APP ID
                .useMediation(true)  //开启聚合功能
                .debug(debug)  //打开debug开关
                .themeStatus(0)  //正常模式  0是正常模式；1是夜间模式；
                .build());
        TTAdSdk.start(new TTAdSdk.Callback() {
            @Override
            public void success() {
                Log.i(TAG, "success: ");
                callback.success();
            }

            @Override
            public void fail(int i, String s) {
                Log.i(TAG, "fail: " + i);
                Log.i(TAG, "fail: " + s);
                callback.fail(i, s);
            }
        });
    }

    public static void shortDrama(Context context, InitCallback callback) {
        if (DJXSdk.isStartSuccess()) {
            callback.success();
            return;
        }
        DJXSdk.init(context, "SDK_Setting_" + Constant.CSJ_APP_ID + ".json", new DJXSdkConfig.Builder()
                .debug(debug)
                .build());
        DJXSdk.start((success, msg, err) -> {
            if (success) {
                callback.success();
            } else {
                if (err == null) err = DJXError.build(1, "DJXSdk start fail");
                callback.fail(err.code, err.msg);
            }
        });
    }

    public static void checkShortDrama(CheckCallback callback) {
        if (DJXSdk.isStartSuccess()) callback.success();
        else {
            Handler handler = new Handler();
            handler.postDelayed(() -> checkShortDrama(callback), 500);
        }
    }

    public static void checkCSJ(CheckCallback callback) {
        if (TTAdSdk.isSdkReady()) callback.success();
        else {
            Handler handler = new Handler();
            handler.postDelayed(() -> checkCSJ(callback), 500);
        }
    }

    public static void checkNovel(CheckCallback callback) {
        if (NovSdk.INSTANCE.isStartSuccess()) callback.success();
        else {
            Handler handler = new Handler();
            handler.postDelayed(() -> checkShortDrama(callback), 500);
        }
    }

    public static void checkVideo(CheckCallback callback) {
        if (DPSdk.isStartSuccess()) callback.success();
        else new Handler().postDelayed(() -> checkVideo(callback), 500);
    }

    public static void smallVideo(Context context, InitCallback callback) {
        if (DPSdk.isStartSuccess()) {
            callback.success();
            return;
        }
        shortDrama(context, new InitCallback() {
            @Override
            public void success() {
                callback.success();
            }

            @Override
            public void fail(int code, String msg) {
                Log.e(TAG, "fail: " + code + ",msg: " + msg);
                callback.fail(code, msg);
            }
        });
    }

    public static void novel(Context context, InitCallback callback) {
        if (NovSdk.INSTANCE.isStartSuccess()) {
            callback.success();
            return;
        }
        NovSdk.init(context, "SDK_Setting_" + Constant.CSJ_APP_ID + ".json", new NovSdkConfig.Builder()
                .debug(debug).build());
        NovSdk.start((b, s, err) -> {
            if (b) {
                callback.success();
            } else {
                if (err != null) {
                    Log.e(TAG, "onStartComplete: " + err);
                }
                callback.fail(-1, s);
            }
        });
    }


    public interface InitCallback {
        void success();

        void fail(int code, String msg);
    }

    public interface CheckCallback {
        void success();
    }
}